<template>
	<view class="action" @tap="setFavorite" style="width: 100rpx;">
		<view :class="is_favorite?'cuIcon-favorfill text-red':'cuIcon-favor'">
		</view>{{count==0?'收藏':count}}
	</view>
</template>

<script>	
	export default {
		data() {
			return {
				id:-1,
				is_favorite:false,
				url:'',
				count:0
			};
		},
		name: 'detail-favorite',
		props: {
			iid:{
				type:Number,
				default:-1
			},
			favCount:{
				type:Number,
				default:0
			},
			isFavorite:{
				type:Boolean,
				default:false
			},
			type:{
				type:Number,
				default:1
			}
		},
		created() {			
			this.id=this.iid
			this.is_favorite=this.isFavorite
			this.count=this.favCount
			if(this.type==1){
				this.url="/scenery/"
			}else{
				this.url="/goods/"
			}
		},
		methods: {
			setFavorite(){
				if(!this.is_favorite){
					this.$api.post(this.url+'add_favorite',{id:this.id}).then(res => {
						this.is_favorite=!this.is_favorite
						this.count++
					}).catch((err)=>{
						uni.showToast({
							title:err.msg,
							icon:'none'
						})
					})
				}else{
					this.$api.post(this.url+'remove_favorite',{id:this.id}).then(res => {
						this.is_favorite=!this.is_favorite
						this.count--
						uni.showToast({
							title:"取消收藏",
							icon:'none'
						})
					}).catch((err)=>{
						uni.showToast({
							title:err.msg,
							icon:'none'
						})
					})
				}
			},
		}
	}
</script>

<style>

</style>
